﻿using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Zhp.Api.Attributes;
using Zhp.Auth;
using Zhp.Entity.SysEnum;
using Zhp.IService.Common;
using Zhp.Model.Common;

namespace Zhp.Api.Controllers.Common
{
    [Route("api/[controller]/[action]")]
    [ApiGroup(GroupName = ApiGroupEnum.Common)]
    [ApiController]
    public class FileController : ControllerBase
    {
        IFileService _fileService;
        public FileController(IFileService fileService)
        {
            _fileService = fileService;
        }

        /// <summary>
        /// 上传文件
        /// </summary>
        /// <param name="param"></param>
        /// <returns></returns>
        [AllowOnline]
        [HttpPost()]
        public async Task<ObjectRet<UploadFileRet>> UploadFile([FromForm] UploadFileParam param)
        {
            return await _fileService.UploadFile(param);
        }

        /// <summary>
        /// 单上传文件，会删除旧文件
        /// </summary>
        /// <param name="param"></param>
        /// <returns></returns>
        [AllowOnline]
        [HttpPost()]
        public async Task<ObjectRet<UploadFileRet>> UploadSingleFile([FromForm] UploadFileParam param)
        {
            return await _fileService.UploadSingleFile(param);
        }

        /// <summary>
        /// 删除文件
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [AllowOnline]
        [HttpGet()]
        public async Task<BaseRet> DeleteFile(long id)
        {
            return await _fileService.DeleteFile(id);
        }

        /// <summary>
        /// 下载文件
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [AllowAnonymous]
        [HttpGet("{id}")]
        public async Task<IActionResult> GetFileStream(long id)
        {
            var (stream, contentType) = await _fileService.GetFileStream(id);
            return File(stream, contentType);
        }

        ///// <summary>
        ///// 查询文件地址
        ///// </summary>
        ///// <param name="module"></param>
        ///// <param name="mainId"></param>
        ///// <returns></returns>
        //[AllowAnonymous]
        //[HttpGet("{module}/{mainId}")]
        //public async Task<BaseRet> GetSingleFileId(FileEnum module, long mainId)
        //{
        //    return new BaseRet { DataExt = await _fileService.GetSingleFileId(module, mainId) };
        //}

        ///// <summary>
        ///// 下载文件
        ///// </summary>
        ///// <param name="module"></param>
        ///// <param name="mainId"></param>
        ///// <returns></returns>
        //[AllowAnonymous]
        //[HttpGet("{module}/{mainId}")]
        //public async Task<ObjectRet<SingleImageFileRet>> GetBase64Image(FileEnum module, long mainId)
        //{
        //    return await _fileService.GetBase64Image(module, mainId);
        //}
    }
}
